package com.beautify.controller;

import com.beautify.entity.dto.CommentDTO;
import com.beautify.entity.dto.ReportPostDTO;
import com.beautify.entity.pojo.Comments;
import com.beautify.entity.pojo.Posts;
import com.beautify.entity.pojo.Result;
import com.beautify.entity.vo.PostVO;
import com.beautify.service.CommentService;
import com.beautify.service.PostsService;
import com.beautify.service.UserService;
import com.beautify.utils.AliOssUtil;
import com.beautify.utils.PostVOFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/user")
public class PostsController {

    @Autowired
    private PostsService postsService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;


    // 视频上传接口
    @PostMapping("/uploadMedia")
    public Result uploadMedia(MultipartFile file) {
        String s = AliOssUtil.uploadMedia(file);
        return Result.success(s);
    }

    //    文件上传接口
    @PostMapping("/upload")
    public Result upload(String userid, final MultipartFile file) throws IOException {
        final String originalFilename = file.getOriginalFilename();
        assert originalFilename != null;
        final String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        final String url = AliOssUtil.uploadFile(filename, file.getInputStream());


        postsService.save(Posts
                .builder()
                .userId(Long.parseLong(userid))
                .username(userService.getByIdToUser(Integer.valueOf(userid)).getUsername())
                .imageUrl(url)
                .build()
        );

        return Result.success(url);
    }

    // 根据盲人ID获取所有所对应帖子接口
    @GetMapping("/allPosts")
    public Result AllPosts(@RequestParam String userid) {
        List<PostVO> list = postsService
                .lambdaQuery()
                .eq(Posts::getUserId, userid)
                .list()
                .stream()
                .map(PostVOFactory::createPostVO)
                .collect(Collectors.toList());
        return Result.success(list);
    }

    // 志愿者查看所有帖子接口
    @GetMapping("/blindPosts")
    public Result volAllPosts() {
        List<PostVO> list = postsService
                .lambdaQuery()
                .list()
                .stream()
                .map(PostVOFactory::createPostVO)
                .collect(Collectors.toList());
        return Result.success(list);
    }

    // 志愿者评论
    @PostMapping("/postComment")
    public Result volPostComment(@RequestBody CommentDTO data) {
        log.info("CommentDTO: {}", data);
        commentService.save(
                Comments.builder()
                        .postId(data.getPostId())
                        .userId(data.getUserId())
                        .content(data.getContent())
                        .build()
        );
        return Result.success();
    }

    // 盲人获取自己发表的帖子的评论
    @GetMapping("/postComments")
    public Result blindPostComments(Integer postsId) {
        List<Comments> list = commentService.lambdaQuery()
                .eq(Comments::getPostId, postsId)
                .list();

        return Result.success(list);
    }

    // 志愿者举报盲人用户帖子接口
    @PostMapping("/reportPost")
    public Result reportPost(@RequestBody ReportPostDTO data) {
        log.info("ReportPostDTO: {}", data);

        // TODO 需要往数据库中添加举报信息

        return Result.success();
    }

}